package com.devmindset.legoframework.usage.client.monitors;

import java.util.List;

import com.devmindset.legoframework.client.mvc.EventMonitor;
import com.devmindset.legoframework.client.mvc.LegoEvent;
import com.devmindset.legoframework.client.mvc.LegoModel;
import com.devmindset.legoframework.client.mvc.metadata.EventMonitorStory;
import com.devmindset.legoframework.client.mvc.metadata.Stories;
import com.devmindset.legoframework.client.mvc.util.ErrorDialog;
import com.devmindset.legoframework.usage.client.component.BannerComponent;
import com.devmindset.legoframework.usage.client.component.CalendarComponent;
import com.devmindset.legoframework.usage.client.component.CountryDetailsComponent;
import com.devmindset.legoframework.usage.client.component.NavigationComponent;
import com.devmindset.legoframework.usage.client.component.OverviewComponent;
import com.devmindset.legoframework.usage.client.component.PendingOrdersComponent;
import com.devmindset.legoframework.usage.client.event.Events;
import com.devmindset.legoframework.usage.client.model.Country;
import com.devmindset.legoframework.usage.client.model.OrderItem;
import com.devmindset.legoframework.usage.client.screen.MainScreen;
import com.devmindset.legoframework.usage.client.service.UsageService;
import com.devmindset.legoframework.usage.client.util.Util;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * Controller for the {@link SmartComponent}s specific to the {@link MainScreen} 
 * of the application.
 * 
 * @author <a href="ice.penov@devmindset.com">Ice Penov</a>
 */
public class MainScreenMonitor extends EventMonitor {
	
	/* Left side column */
	private NavigationComponent navigationComponent = new NavigationComponent();
	private BannerComponent bannerComponent = new BannerComponent();
	private CalendarComponent calendarComponent = new CalendarComponent();
	/* Main area components */
	private OverviewComponent overviewComponent = new OverviewComponent();
	private PendingOrdersComponent pendingOrdersComponent = new PendingOrdersComponent();
	private CountryDetailsComponent countryDetailsComponent = new CountryDetailsComponent();
	
	@Override
	public void handleEvent(LegoEvent event) {
		if (event.matches(Events.RENDER_NAVIGATION_COMPONENT)) {
			navigationComponent.render(new LegoModel(null));
		} else if (event.matches(Events.RENDER_OVERVIEW_COMPONENT)) {
			overviewComponent.render(new LegoModel(null));
		} else if (event.matches(Events.RENDER_PENDING_ORDERS_COMPONENT)) {
			UsageService.get().getOrderItems(new AsyncCallback<List<OrderItem>>() {
				public void onFailure(Throwable arg0) {
					ErrorDialog.logAndShow(arg0);
				}
				public void onSuccess(List<OrderItem> list) {
					pendingOrdersComponent.render(new LegoModel(Util.toOrderItemRecords(list)));
				}
			});
		} else if (event.matches(Events.RENDER_COUNTRY_DETAILS_COMPONENT)) {
			UsageService.get().getCountries(new AsyncCallback<List<Country>>() {
				public void onSuccess(List<Country> list) {
					countryDetailsComponent.render(new LegoModel(Util.toCountryRecords(list)));
				}
				public void onFailure(Throwable arg0) {
					ErrorDialog.logAndShow(arg0);
				}
			});
		} else if (event.matches(Events.RENDER_BANNER_COMPONENT)) {
			bannerComponent.render(new LegoModel(null));
		} else if (event.matches(Events.RENDER_CALENDAR_COMPONENT)) {
			calendarComponent.render(new LegoModel(null));
		}
	}

	public EventMonitorStory myStory() {
		return Stories.eventMonitor()
			.canHandle(
					Events.RENDER_NAVIGATION_COMPONENT,
					Events.RENDER_OVERVIEW_COMPONENT,
					Events.RENDER_PENDING_ORDERS_COMPONENT,
					Events.RENDER_COUNTRY_DETAILS_COMPONENT,
					Events.RENDER_BANNER_COMPONENT,
					Events.RENDER_CALENDAR_COMPONENT
			)
			.knownAs("Main Application Screen Monitor");
	}
	
}